package com.mwumz.third.service;

import com.alibaba.fastjson.JSONObject;
import com.mwumz.third.config.DispatcherProperties;
import com.mwumz.third.handler.HttpClientHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wumz
 * @Date: 2020/7/2 15:20
 * @Version 1.0
 */
@Service
public class ScottMapService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScottMapService.class);

    @Autowired
    private DispatcherProperties dispatcherProperties;

    @Autowired
    private HttpClientHandler httpClientHandler;

    /**
     * 地理编码 API 服务地址
     * https://restapi.amap.com/v3/geocode/geo?parameters
     * @param address
     * @return
     */
    public JSONObject getLocation(String address) {
        JSONObject result = null;
        String url = dispatcherProperties.getGaodeUri() + dispatcherProperties.getGeoPath();
        Map<String,String> params = new HashMap<>(6);
        params.put("key",dispatcherProperties.getGaodeKey());
        params.put("city","");//无，会进行全国范围内搜索
        params.put("batch","true");
        params.put("output","JSON");
        params.put("address",address);
        params.put("callback","");
        try{
            String response = httpClientHandler.doGet(url,params);
            LOGGER.debug("getLocation response:{}",response);
            result = JSONObject.parseObject(response);
        }catch (Exception e){
            LOGGER.error("getLocation error:{} ",e.getMessage());
        }
        return result;
    }
}
